package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.DwtzQtqk;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class DwtzQtqkForm extends BasePage implements Serializable {
    private GenericManager<DwtzQtqk, Long> dwtzQtqkManager;
    private DwtzQtqk dwtzQtqk = new DwtzQtqk();
    private Long id;
    private Jbqk jbqk;
    
    public void setJbqk(Jbqk jbqk)
    {
    	this.jbqk=jbqk;
    }

    public void setDwtzQtqkManager(GenericManager<DwtzQtqk, Long> dwtzQtqkManager) {
        this.dwtzQtqkManager = dwtzQtqkManager;
    }

    public DwtzQtqk getDwtzQtqk() {
        return dwtzQtqk;
    }

    public void setDwtzQtqk(DwtzQtqk dwtzQtqk) {
        this.dwtzQtqk = dwtzQtqk;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        dwtzQtqkManager.remove(dwtzQtqk.getId());
        addMessage("dwtzQtqk.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            dwtzQtqk = dwtzQtqkManager.get(id);
        } else {
            dwtzQtqk = new DwtzQtqk();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (dwtzQtqk.getId() == null || dwtzQtqk.getId() == 0);
        
        dwtzQtqk.setJbqk(jbqk);
        dwtzQtqk.setUsername(getRequest().getRemoteUser());
        dwtzQtqk.setOptdate(new Date());
        dwtzQtqk.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        
        dwtzQtqkManager.save(dwtzQtqk);

        String key = (isNew) ? "dwtzQtqk.added" : "dwtzQtqk.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id=this.dwtzQtqk.getId();
            return edit();
        }
    }
} 